package net.jforum.view.forum;
/**
 * @ author  张洋 jackson.zhang@ericsson.com
 * @ version 创建时间 ：Aug 4, 2010  12:21:52 AM
 * @ description : 
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import net.jforum.Command;
import net.jforum.JForumExecutionContext;
import net.jforum.entities.Forum;
import net.jforum.entities.Topic;
import net.jforum.repository.ForumRepository;
import net.jforum.repository.SummaryRepository;
import net.jforum.repository.TopicRepository;
import net.jforum.util.I18n;
import net.jforum.util.SEOUtil;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;
import net.jforum.util.preferences.TemplateKeys;
import net.jforum.view.forum.common.ForumCommon;
import net.jforum.view.forum.common.TopicsCommon;
import net.jforum.view.forum.common.ViewCommon;

/**
 * Display a list of recent Topics
 * 
 * @author James Yong
 * @author Rafael Steil
 * @version $Id: RecentTopicsAction.java,v 1.20 2007/08/20 19:35:52 rafaelsteil Exp $
 */
public class AllTopicsAction extends Command 
{
	private List forums;
	private static Logger logger = Logger.getLogger(SummaryRepository.class);
	public void list()
	{   

		int start = ViewCommon.getStartPage();
	 	int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
 		int totalTopics =TopicRepository.getTopicsCount() ;
         int postPerPage=SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
        int displayPages=SystemGlobals.getIntValue(ConfigKeys.ALLTOPIC_DISPLAY_PAGE);
        if(totalTopics/topicsPerPage>displayPages){
        	totalTopics=displayPages*topicsPerPage;
        }
		ViewCommon.contextToPagination(start, totalTopics, topicsPerPage);
		this.context.put("postsPerPage", new Integer(postPerPage));
//		this.context.put("recordsPerPage", new Integer(topicsPerPage));
		this.setTemplateName(TemplateKeys.ALLTOPICS_LIST);
		
	//	this.context.put("postsPerPage", new Integer(topicsPerPage));
		 
		String selectIndexId=this.request.getParameter("selectIndex");

		if("1".equals(selectIndexId)){//发帖时间排序
			this.context.put("topics",this.topics(1,start, topicsPerPage));
		}else if("2".equals(selectIndexId)){//回复次数
			this.context.put("topics", this.topics(2,start, topicsPerPage));
		}else {
			this.context.put("topics", this.topics(0,start,topicsPerPage));
		}
		if(StringUtils.isBlank(selectIndexId))
		   this.context.put("selectIndex", new Integer(0));
		else 
		   this.context.put("selectIndex",new Integer(selectIndexId));
		
		
		
		
// 		this.context.put("topics", this.topics(start/topicsPerPage));
		this.context.put("forums", this.forums);
		this.context.put("pageTitle", SEOUtil.forumShowSEO(I18n.getMessage("ForumBase.allForum")));
		this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false)); 
        this.context.put("module", "allTopics");
		TopicsCommon.topicListingBase();
		this.request.setAttribute("template", null);
	}
	
	private List topics(int selectIndex,int start,int topicsPerPage)
	{   List topics=new ArrayList();
 	if(selectIndex==0)		
		  topics = TopicRepository.getTopicsByLimit(start,topicsPerPage);
 
		else if(selectIndex==1){
		  topics=TopicsCommon.getAllTopicsByTopicPublishTime(start, topicsPerPage);
		}else {
		  topics=TopicsCommon.selectAllTopicsByReplyCount(start, topicsPerPage);
		}
 
		this.forums = new ArrayList(topicsPerPage);

		for (Iterator iter = topics.iterator(); iter.hasNext(); ) {
			Topic t = (Topic)iter.next();
			
			if (TopicsCommon.isTopicAccessible(t.getForumId())) {
				Forum f = ForumRepository.getForum(t.getForumId());
				forums.add(f);
			}
			else {
				iter.remove();
			}
		}
		
		JForumExecutionContext.getRequest().removeAttribute("template");
		
		return TopicsCommon.prepareTopics(topics);
	}
}
